//
//  YWMessageManager.m
//  maintain
//
//  Created by Kyo on 16/4/22.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWMessageManager.h"
#import "YWDBTool.h"
#import "YWMessage.h"
#import "YWMacro.h"
#import "YWCommonTool.h"
#import "YWLoginTool.h"
#import "YWLoginUser.h"
#import "YWPush.h"
#import "YWMissionManager.h"
#import "YWEquipment.h"
#import "YWEquipmentManager.h"
#import "YWTempMissionManager.h"
#import "YWNoticeManager.h"
#import "YWKeepWatchMissionManager.h"
#import "YWInspectionMissionManager.h"

@implementation YWMessageManager
YWSingletonM(Manager)

-(BOOL)updateMessage:(YWMessage *)message{
    BOOL save = [[YWDBTool shareDBTool] updateMessage:message];
    if (save) {
        //推送的提醒消息
        [YWLoginTool savePushMessageNoRedCount];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"pushMessageNoReadCountNotification" object:nil];
    }
    return save;
}

-(BOOL)updateMessageWithArray:(NSArray *)messageArray{
    return [[YWDBTool shareDBTool] updateMessageWithArray:messageArray];
}

-(YWMessage *)theLatestMessage{
    return [[YWDBTool shareDBTool] theLatestMessage];
}
- (void)saveMessageWithPushMessage:(id)pushMessage{
    __block YWMessage *message = nil;
//    YWWeakSelf(weakSelf);
    if ([pushMessage isKindOfClass:[YWPushMission class]]) {
        YWPushMission *pushMission = (YWPushMission *)pushMessage;
        YWMission *mission = [[YWMissionManager shareManager] missionWithMissionId:pushMission.missionId maintenanceType:pushMission.maintenanceTypeId];
        NSString *content = @"";
        if (pushMission.missionStatus == YWMissionStatusUnfinished) {
            content = [NSString stringWithFormat:@"我的任务#%@#有了新的进度,任务要求检测数量:%ld个,已完成:%ld个",pushMission.missionName?:@"",(long)pushMission.equipmentQuantity,(long)pushMission.completedQuantity];
        }else{
            content = [NSString stringWithFormat:@"我的任务#%@#有了新的进度,已完成",pushMission.missionName?:@""];
        }
//        if ([mission.executorIds rangeOfString:[NSString stringWithFormat:@"%ld",(long)[YWLoginTool loginUser].uniqueId]].location != NSNotFound) {
//            if (pushMission.executor.uniqueId == [YWLoginTool loginUser].uniqueId) {
//                //1自己是执行者
//                message = [self createMessageWithContent:content title:@"提醒" fromUserId:pushMission.executor.uniqueId sessionId:mission.creator.uniqueId messageType:YWMessageTypePushMessage relationId:[NSString stringWithFormat:@"%ld",(long)pushMission.missionId] relationType:pushMission.maintenanceTypeId];
//                
//            }else{
//                //2,自己是执行者之一
//                message = [self createMessageWithContent:content title:@"提醒" fromUserId:pushMission.executor.uniqueId sessionId:pushMission.executor.uniqueId messageType:YWMessageTypePushMessage relationId:[NSString stringWithFormat:@"%ld",(long)pushMission.missionId] relationType:pushMission.maintenanceTypeId];
//            }
//        }else{
//            //3自己是创建者
//            message = [self createMessageWithContent:content title:@"提醒" fromUserId:pushMission.executor.uniqueId sessionId:pushMission.executor.uniqueId messageType:YWMessageTypePushMessage relationId:[NSString stringWithFormat:@"%ld",(long)pushMission.missionId] relationType:pushMission.maintenanceTypeId];
//        }
        message = [self createMessageWithContent:content title:@"提醒" fromUserId:pushMission.executor.uniqueId messageType:YWMessageTypePushMessage relationId:[NSString stringWithFormat:@"%ld",(long)pushMission.missionId] relationType:pushMission.maintenanceTypeId];
        if (pushMission.executor.uniqueId != [YWLoginTool loginUser].uniqueId) {
            [self performSelectorOnMainThread:@selector(saveAndPostNotificationWithMessage:) withObject:message waitUntilDone:NO];
        }else{
            [self performSelectorOnMainThread:@selector(updateMessage:) withObject:message waitUntilDone:NO];
        }
        
        if (mission) {
            [[YWMissionManager shareManager]requestMissionWithMissionId:mission.missionId completeBlock:nil];
        }
        
    }else if ([pushMessage isKindOfClass:[YWPushTrouble class]]){
        YWPushTrouble *pushTrouble = (YWPushTrouble *)pushMessage;
        YWMission *mission = [[YWMissionManager shareManager] missionWithMissionId:pushTrouble.troubleId maintenanceType:YWMaintenanceTypeTrouble];
        YWEquipment *equipment = [[YWEquipmentManager shareManager] equipmentWithId:mission.equipmentId];
        if (mission && equipment) {
            NSString *content = [NSString stringWithFormat:@"我完成了维修任务#维修%@#%@",equipment.equipmentName?:@"",mission.note?:@""];
            message = [self createMessageWithContent:content title:@"提醒" fromUserId:mission.executor.uniqueId messageType:YWMessageTypePushMessage relationId:[NSString stringWithFormat:@"%ld",(long)mission.missionId] relationType:YWMaintenanceTypeTrouble];
            
            if (mission.executor.uniqueId != [YWLoginTool loginUser].uniqueId) {
                [self performSelectorOnMainThread:@selector(saveAndPostNotificationWithMessage:) withObject:message waitUntilDone:NO];
            }else{
                [self performSelectorOnMainThread:@selector(updateMessage:) withObject:message waitUntilDone:NO];
            }
            [[YWMissionManager shareManager]requestTroubleMissionWithTroubleId:pushTrouble.troubleId withComplete:nil];
        }else{
            [[YWMissionManager shareManager]requestTroubleMissionWithTroubleId:pushTrouble.troubleId withComplete:^(YWMission *troubleMission) {
                if (troubleMission) {
                    NSString *content = [NSString stringWithFormat:@"我完成了维修任务#维修%@#%@",equipment.equipmentName?:@"",troubleMission.note?:@""];
                    message = [self createMessageWithContent:content title:@"提醒" fromUserId:troubleMission.executor.uniqueId messageType:YWMessageTypePushMessage relationId:[NSString stringWithFormat:@"%ld",(long)troubleMission.missionId] relationType:YWMaintenanceTypeTrouble];
                    
                    if (troubleMission.executor.uniqueId != [YWLoginTool loginUser].uniqueId) {
                        [self performSelectorOnMainThread:@selector(saveAndPostNotificationWithMessage:) withObject:message waitUntilDone:NO];
                    }else{
                        [self performSelectorOnMainThread:@selector(updateMessage:) withObject:message waitUntilDone:NO];
                    }
                }
            }];
        }
        
    }else if ([pushMessage isKindOfClass:[YWPushAssign class]]){
        YWPushAssign *pushAssign = (YWPushAssign *)pushMessage;
        if (pushAssign.troubleId>0) {
            [[YWMissionManager shareManager]requestTroubleMissionWithTroubleId:pushAssign.troubleId withComplete:^(YWMission *mission) {
                if (mission) {
                    YWEquipment *equipment = [[YWEquipmentManager shareManager] equipmentWithId:mission.equipmentId];
                    NSString *content = [NSString stringWithFormat:@"我分配给你一条新的维修任务#维修%@#%@",equipment.equipmentName?:@"",mission.note?:@""];
                    if (mission.executor.uniqueId != [YWLoginTool loginUser].uniqueId && mission.reporterId == [YWLoginTool loginUser].uniqueId) {
                        if (mission.executor.userName.length) {
                            content = [NSString stringWithFormat:@"你上报的故障#维修%@#已经分配给了%@",equipment.equipmentName?:@"",mission.executor.userName];
                        }else{
                            content = [NSString stringWithFormat:@"你上报的故障#维修%@#已经被分配了",equipment.equipmentName?:@""];
                        }
                    }
                    
                    YWMessage * message = [self createMessageWithContent:content title:@"提醒" fromUserId:mission.creator.uniqueId messageType:YWMessageTypePushMessage relationId:[NSString stringWithFormat:@"%ld",(long)mission.missionId] relationType:YWMaintenanceTypeTrouble];
                    
                    if (mission.creator.uniqueId != [YWLoginTool loginUser].uniqueId) {
                        [self performSelectorOnMainThread:@selector(saveAndPostNotificationWithMessage:) withObject:message waitUntilDone:NO];
                    }else{
                        [self performSelectorOnMainThread:@selector(updateMessage:) withObject:message waitUntilDone:NO];
                    }
                }
            }];
        }
    }else if ([pushMessage isKindOfClass:[YWPushReport class]]){
        YWPushReport *pushReport = (YWPushReport *)pushMessage;
        if (pushReport.troubleId>0) {
            [[YWMissionManager shareManager]requestTroubleMissionWithTroubleId:pushReport.troubleId withComplete:^(YWMission *mission) {
                if (mission) {
                    NSString *content = nil;
                    YWEquipment *equipment = [[YWEquipmentManager shareManager] equipmentWithId:mission.equipmentId];
                    content = [NSString stringWithFormat:@"我上报了一个故障,故障设备#%@#%@",equipment.equipmentName?:@"",mission.note?:@""];
                    
                    YWMessage * message = [self createMessageWithContent:content title:@"提醒" fromUserId:mission.reporterId messageType:YWMessageTypePushMessage relationId:[NSString stringWithFormat:@"%ld",(long)mission.missionId] relationType:YWMaintenanceTypeTrouble];
                    
                    if (mission.reporterId != [YWLoginTool loginUser].uniqueId) {
                        [self performSelectorOnMainThread:@selector(saveAndPostNotificationWithMessage:) withObject:message waitUntilDone:NO];
                    }else{
                        [self performSelectorOnMainThread:@selector(updateMessage:) withObject:message waitUntilDone:NO];
                    }
                }
            }];
        }
    }else if ([pushMessage isKindOfClass:[YWPushCommonMission class]]){
        YWPushCommonMission *commonMission = (YWPushCommonMission *)pushMessage;
        if (commonMission.missionId) {
            NSString *typeName = @"维保";
            if (commonMission.maintenanceTypeId == YWMaintenanceTypeDetection) {
                typeName = @"检测";
            }else if (commonMission.maintenanceTypeId == YWMaintenanceTypeUpkeep){
                typeName = @"保养";
            }
            NSString *content = [NSString stringWithFormat:@"我分配给你了一条%@任务#%@#",typeName,commonMission.missionName?:@""];
            message = [self createMessageWithContent:content title:@"提醒" fromUserId:commonMission.creator.uniqueId messageType:YWMessageTypePushMessage relationId:[NSString stringWithFormat:@"%ld",(long)commonMission.missionId] relationType:commonMission.maintenanceTypeId];
            
            //自己是分配人就屏蔽,不在最新消息显示，但在工作提醒里有(即保存但不发送通知)
            if (commonMission.creator.uniqueId != [YWLoginTool loginUser].uniqueId) {
                [self performSelectorOnMainThread:@selector(saveAndPostNotificationWithMessage:) withObject:message waitUntilDone:NO];
            }else{
                [self performSelectorOnMainThread:@selector(updateMessage:) withObject:message waitUntilDone:NO];
            }
            
            [[YWMissionManager shareManager]requestMissionWithMissionId:commonMission.missionId completeBlock:nil];
        }
        
    }else if ([pushMessage isKindOfClass:[YWPushWorkTask class]]){
        //临时任务通知
        YWPushWorkTask *pushWrokTask = (YWPushWorkTask *)pushMessage;
        if (pushWrokTask.workTaskId>0) {
            [self requestWorkTask:pushWrokTask.workTaskId];
        }
    }else if ([pushMessage isKindOfClass:[YWPushWorkTaskFinish class]]){
        //完成临时任务的回复
        YWPushWorkTaskFinish *workTaskFinish = (YWPushWorkTaskFinish *)pushMessage;
        [self requestReplyOfWorkTask:workTaskFinish];
    }
    else if ([pushMessage isKindOfClass:[YWPushWorkNotice class]]){
        //公告
        YWPushWorkNotice *pushPublicNotice = (YWPushWorkNotice *)pushMessage;
        [self requestPublicNotice:pushPublicNotice];
    }
    //
    else if ([pushMessage isKindOfClass:[YWPushCancelMission class]]){
        YWPushCancelMission *pushCancelMission = (YWPushCancelMission *)pushMessage;
        //因为服务端的变动没有返回任务名称,任务分配人
        YWMission *mission = [[YWMissionManager shareManager] missionWithMissionId:pushCancelMission.missionId maintenanceType:pushCancelMission.maintenanceTypeId];
        if (mission) {
            [[YWMissionManager shareManager] deleteMissionWithMissionId:pushCancelMission.missionId maintenanceType:pushCancelMission.maintenanceTypeId];
            NSString *content = [NSString stringWithFormat:@"我取消了分配给你的任务#%@#",mission.missionName?:@""];
            message = [self createMessageWithContent:content title:@"提醒" fromUserId:mission.creator.uniqueId messageType:YWMessageTypePushMessage relationId:[NSString stringWithFormat:@"%ld",(long)mission.missionId] relationType:mission.maintenanceTypeId];
            
            if (mission.creator.uniqueId != [YWLoginTool loginUser].uniqueId) {
                [self performSelectorOnMainThread:@selector(saveAndPostNotificationWithMessage:) withObject:message waitUntilDone:NO];
            }else{
                [self performSelectorOnMainThread:@selector(updateMessage:) withObject:message waitUntilDone:NO];
            }
        }
        
    }else if ([pushMessage isKindOfClass:[YWPushCancelTrouble class]]){
        YWPushCancelTrouble *pushCancelTrouble = (YWPushCancelTrouble *)pushMessage;
        if (pushCancelTrouble.troubleId>0) {
            YWMission *mission = [[YWMissionManager shareManager] missionWithMissionId:pushCancelTrouble.troubleId maintenanceType:YWMaintenanceTypeTrouble];
            [[YWMissionManager shareManager] deleteMissionWithMissionId:pushCancelTrouble.troubleId maintenanceType:YWMaintenanceTypeTrouble];
            YWEquipment *equipment = [[YWEquipmentManager shareManager] equipmentWithId:mission.equipmentId];
            if (mission && equipment) {
                NSString *content = [NSString stringWithFormat:@"我取消了分配给你的维修任务#维修%@#",equipment.equipmentName?:@""];
                message = [self createMessageWithContent:content title:@"提醒" fromUserId:mission.creator.uniqueId messageType:YWMessageTypePushMessage relationId:[NSString stringWithFormat:@"%ld",(long)mission.missionId] relationType:YWMaintenanceTypeTrouble];
                
                if (mission.creator.uniqueId != [YWLoginTool loginUser].uniqueId) {
                    [self performSelectorOnMainThread:@selector(saveAndPostNotificationWithMessage:) withObject:message waitUntilDone:NO];
                }else{
                    [self performSelectorOnMainThread:@selector(updateMessage:) withObject:message waitUntilDone:NO];
                }
            }
        }
    }else if ([pushMessage isKindOfClass:[YWPushMissionModify class]]){
        YWPushMissionModify *missionModify = (YWPushMissionModify *)pushMessage;
        YWMission *mission = [[YWMissionManager shareManager] missionWithMissionId:missionModify.missionId maintenanceType:missionModify.maintenanceTypeId];
        NSString *typeName = @"维保";
        if (missionModify.maintenanceTypeId == YWMaintenanceTypeDetection) {
            typeName = @"检测";
        }else if (missionModify.maintenanceTypeId == YWMaintenanceTypeUpkeep){
            typeName = @"保养";
        }
        NSString *content = [NSString stringWithFormat:@"%@任务#%@#有变动,请及时查看",typeName,mission.missionName?:@""];
        
        message = [self createMessageWithContent:content title:@"提醒" fromUserId:missionModify.creator.uniqueId messageType:YWMessageTypePushMessage relationId:[NSString stringWithFormat:@"%ld",(long)mission.missionId] relationType:mission.maintenanceTypeId];
        
        if (missionModify.creator.uniqueId != [YWLoginTool loginUser].uniqueId) {
            [self performSelectorOnMainThread:@selector(saveAndPostNotificationWithMessage:) withObject:message waitUntilDone:NO];
        }else{
            [self performSelectorOnMainThread:@selector(updateMessage:) withObject:message waitUntilDone:NO];
        }
        if (mission) {
            [[YWMissionManager shareManager]requestMissionWithMissionId:mission.missionId completeBlock:nil];
        }
    }else if ([pushMessage isKindOfClass:[YWPushKeepWatchMission class]]){
        YWPushKeepWatchMission *pushKeepWatchMission = (YWPushKeepWatchMission*)pushMessage;
        
        [[YWKeepWatchMissionManager shareManager] requestKeepWatchMissionWithMissionId:pushKeepWatchMission.keepWatchMissionId Complete:^(BOOL success) {
            YWKeepWatchMission *keepWatchMission = [[YWKeepWatchMissionManager shareManager]keepWatchMissionWithMissionId:pushKeepWatchMission.keepWatchMissionId];
            NSString *content = [NSString stringWithFormat:@"我发给了你一条新的巡更任务#%@#请在每天规定的时间内进行巡更",keepWatchMission.patrolLine.patrolLineName?:@""];
            YWMessage * message = [self createMessageWithContent:content title:@"提醒" fromUserId:keepWatchMission.creator.uniqueId messageType:YWMessageTypePushMessage relationId:[NSString stringWithFormat:@"%ld",(long)keepWatchMission.keepWatchMissionId] relationType:YWPushMessageRelationTypeKeepWatch];
            
            if (keepWatchMission.creator.uniqueId != [YWLoginTool loginUser].uniqueId) {
                [self performSelectorOnMainThread:@selector(saveAndPostNotificationWithMessage:) withObject:message waitUntilDone:NO];
            }else{
                [self performSelectorOnMainThread:@selector(updateMessage:) withObject:message waitUntilDone:NO];
            }
        }];
    }else if ([pushMessage isKindOfClass:[YWPushFinishKeepWatchMission class]]){
        YWPushFinishKeepWatchMission *pushFinishKeepWatchMission = (YWPushFinishKeepWatchMission*)pushMessage;
        YWKeepWatchMission *keepWatchMission = [[YWKeepWatchMissionManager shareManager]keepWatchMissionWithMissionId:pushFinishKeepWatchMission.keepWatchMissionId];
        NSString *content = [NSString stringWithFormat:@"我完成了一次巡更任务#%@#",keepWatchMission.patrolLine.patrolLineName?:@""];
        YWMessage * message = [self createMessageWithContent:content title:@"提醒" fromUserId:pushFinishKeepWatchMission.executor.uniqueId messageType:YWMessageTypePushMessage relationId:[NSString stringWithFormat:@"%ld",(long)keepWatchMission.keepWatchMissionId] relationType:YWPushMessageRelationTypeKeepWatchRecord];
        
        if (pushFinishKeepWatchMission.executor.uniqueId != [YWLoginTool loginUser].uniqueId) {
            [self performSelectorOnMainThread:@selector(saveAndPostNotificationWithMessage:) withObject:message waitUntilDone:NO];
        }else{
            [self performSelectorOnMainThread:@selector(updateMessage:) withObject:message waitUntilDone:NO];
        }
    }else if ([pushMessage isKindOfClass:[YWPushModifyKeepWatchMission class]]){
        YWPushModifyKeepWatchMission *modifyKeepWatchMission = (YWPushModifyKeepWatchMission *)pushMessage;
        YWKeepWatchMission *keepWatchMission = [[YWKeepWatchMissionManager shareManager]keepWatchMissionWithMissionId:modifyKeepWatchMission.keepWatchMissionId];
        NSString *content = [NSString stringWithFormat:@"巡更任务#%@#有新的改动，请注意查看",keepWatchMission.patrolLine.patrolLineName?:@""];
        YWMessage * message = [self createMessageWithContent:content title:@"提醒" fromUserId:keepWatchMission.creator.uniqueId messageType:YWMessageTypePushMessage relationId:[NSString stringWithFormat:@"%ld",(long)keepWatchMission.keepWatchMissionId] relationType:YWPushMessageRelationTypeKeepWatch];
        
        if (keepWatchMission.creator.uniqueId != [YWLoginTool loginUser].uniqueId) {
            [self performSelectorOnMainThread:@selector(saveAndPostNotificationWithMessage:) withObject:message waitUntilDone:NO];
        }else{
            [self performSelectorOnMainThread:@selector(updateMessage:) withObject:message waitUntilDone:NO];
        }
        [[YWKeepWatchMissionManager shareManager] requestKeepWatchMissionWithMissionId:modifyKeepWatchMission.keepWatchMissionId Complete:nil];
        
    }else if ([pushMessage isKindOfClass:[YWPushCancelKeepWatchMission class]]){
        YWPushCancelKeepWatchMission *cancelKeepWatchMission = (YWPushCancelKeepWatchMission *)pushMessage;
        YWKeepWatchMission *keepWatchMission = [[YWKeepWatchMissionManager shareManager]keepWatchMissionWithMissionId:cancelKeepWatchMission.keepWatchMissionId];
        NSString *content = [NSString stringWithFormat:@"我取消了分配给你的巡更任务#%@#",keepWatchMission.patrolLine.patrolLineName?:@""];
        YWMessage * message = [self createMessageWithContent:content title:@"提醒" fromUserId:keepWatchMission.creator.uniqueId messageType:YWMessageTypePushMessage relationId:[NSString stringWithFormat:@"%ld",(long)keepWatchMission.keepWatchMissionId] relationType:YWPushMessageRelationTypeKeepWatch];
        
        if (keepWatchMission.creator.uniqueId != [YWLoginTool loginUser].uniqueId) {
            [self performSelectorOnMainThread:@selector(saveAndPostNotificationWithMessage:) withObject:message waitUntilDone:NO];
        }else{
            [self performSelectorOnMainThread:@selector(updateMessage:) withObject:message waitUntilDone:NO];
        }
        [[YWKeepWatchMissionManager shareManager] requestKeepWatchMissionWithComplete:nil];
    }else if ([pushMessage isKindOfClass:[YWPushInspectionMission class]]){
        YWPushInspectionMission *pushInspectionMission = (YWPushInspectionMission*)pushMessage;
        [[YWInspectionMissionManager shareManager] requestInspectionMissionWithMissionId:pushInspectionMission.inspectionMissionId Complete:^(YWInspectionMissionModel *inspectionMission) {
            if (inspectionMission) {
                NSString *content = [NSString stringWithFormat:@"我发给了你一条新的巡查任务#%@#",inspectionMission.inspectionMissionName?:@""];
                YWMessage * message = [self createMessageWithContent:content title:@"提醒" fromUserId:inspectionMission.creator.uniqueId messageType:YWMessageTypePushMessage relationId:[NSString stringWithFormat:@"%ld",(long)inspectionMission.inspectionMissionId] relationType:YWPushMessageRelationTypeInspection];
                
                if (inspectionMission.creator.uniqueId != [YWLoginTool loginUser].uniqueId) {
                    [self performSelectorOnMainThread:@selector(saveAndPostNotificationWithMessage:) withObject:message waitUntilDone:NO];
                }else{
                    [self performSelectorOnMainThread:@selector(updateMessage:) withObject:message waitUntilDone:NO];
                }
            }
        }];
    }else if ([pushMessage isKindOfClass:[YWPushCancelInspectionMission class]]){
        YWPushCancelInspectionMission *cancelInspectionMission = (YWPushCancelInspectionMission *)pushMessage;
        YWInspectionMissionModel *inspectionMission = [[YWInspectionMissionManager shareManager] inspectionMissionWithMissionId:cancelInspectionMission.inspectionMissionId];
        if (inspectionMission) {
            NSString *content = [NSString stringWithFormat:@"我取消了分配给你的巡查任务#%@#",inspectionMission.inspectionMissionName?:@""];
            YWMessage * message = [self createMessageWithContent:content title:@"提醒" fromUserId:inspectionMission.creator.uniqueId messageType:YWMessageTypePushMessage relationId:[NSString stringWithFormat:@"%ld",(long)inspectionMission.inspectionMissionId] relationType:YWPushMessageRelationTypeInspection];
            
            if (inspectionMission.creator.uniqueId != [YWLoginTool loginUser].uniqueId) {
                [self performSelectorOnMainThread:@selector(saveAndPostNotificationWithMessage:) withObject:message waitUntilDone:NO];
            }else{
                [self performSelectorOnMainThread:@selector(updateMessage:) withObject:message waitUntilDone:NO];
            }
        }
        [[YWInspectionMissionManager shareManager] requestInspectionMissionWithComplete:nil];
        
    }else if ([pushMessage isKindOfClass:[YWPushModifyInspectionMission class]]){
        YWPushModifyInspectionMission *modifyInspection = (YWPushModifyInspectionMission*)pushMessage;
        YWInspectionMissionModel *inspectionMission = [[YWInspectionMissionManager shareManager] inspectionMissionWithMissionId:modifyInspection.inspectionMissionId];
        NSString *content = [NSString stringWithFormat:@"巡查任务#%@#有变动,请注意查看",inspectionMission.inspectionMissionName?:@""];
        YWMessage * message = [self createMessageWithContent:content title:@"提醒" fromUserId:inspectionMission.creator.uniqueId messageType:YWMessageTypePushMessage relationId:[NSString stringWithFormat:@"%ld",(long)inspectionMission.inspectionMissionId] relationType:YWPushMessageRelationTypeInspection];
        
        if (inspectionMission.creator.uniqueId != [YWLoginTool loginUser].uniqueId) {
            [self performSelectorOnMainThread:@selector(saveAndPostNotificationWithMessage:) withObject:message waitUntilDone:NO];
        }else{
            [self performSelectorOnMainThread:@selector(updateMessage:) withObject:message waitUntilDone:NO];
        }
        [[YWInspectionMissionManager shareManager] requestInspectionMissionWithMissionId:modifyInspection.inspectionMissionId Complete:nil];
    }else if ([pushMessage isKindOfClass:[YWPushInspectionMissionProgress class]]){
        YWPushInspectionMissionProgress *inspectionProgress = (YWPushInspectionMissionProgress*)pushMessage;
        YWInspectionMissionModel *inspectionMission = [[YWInspectionMissionManager shareManager] inspectionMissionWithMissionId:inspectionProgress.inspectionMissionId];
        NSString *content = nil;
        NSArray *intervalArray = [YWCommonTool startTimeIntervalAndEndTimeInterval:inspectionMission];
        NSTimeInterval startInterval = [[intervalArray firstObject] longLongValue];
        NSTimeInterval endInterval = [[intervalArray lastObject] longLongValue];
        
        NSArray *judgeArray = [YWCommonTool judgeCurrentTimeSpaceWithStartTime:startInterval endTime:endInterval interval:inspectionMission.inspectionLine.intervalMinute];
        NSTimeInterval lineRecordStarTime = (judgeArray?[[judgeArray firstObject] longLongValue]:0)/1000;
        YWInspectionLineRecord *record = [[YWInspectionMissionManager shareManager]latestInspectionLineRecordWithInspectionLineId:inspectionMission.inspectionLine.inspectionLineId inspectionMissionId:inspectionMission.inspectionMissionId andStatu:YWInspectionLineRecordStatusALL startTime:lineRecordStarTime];
        NSArray *recordData = [[YWInspectionMissionManager shareManager] inspectionEquipmentRecordWithUniqueTag:record.uniqueTag];
        if (recordData.count == inspectionMission.inspectionLine.equipments.count) {
            content = [NSString stringWithFormat:@"巡查任务#%@#有了新进度,已完成",inspectionMission.inspectionMissionName?:@""];
        }else{
            content = [NSString stringWithFormat:@"巡查任务#%@#有了新进度,请注意查看",inspectionMission.inspectionMissionName?:@""];
            
        }
        
        YWMessage * message = [self createMessageWithContent:content title:@"提醒" fromUserId:inspectionProgress.executor.uniqueId messageType:YWMessageTypePushMessage relationId:inspectionProgress.uniqueTag?:@"" relationType:YWPushMessageRelationTypeInspectionRecord];
        
        if (inspectionProgress.executor.uniqueId != [YWLoginTool loginUser].uniqueId) {
            [self performSelectorOnMainThread:@selector(saveAndPostNotificationWithMessage:) withObject:message waitUntilDone:NO];
        }else{
            [self performSelectorOnMainThread:@selector(updateMessage:) withObject:message waitUntilDone:NO];
        }
    }
    
}
- (void)saveAndPostNotificationWithMessage:(YWMessage*)message{
    //保存数据库
    BOOL save = [self updateMessage:message];
    //发通知更新最新消息
    if (save) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"reflashTheLatestMessage" object:nil];
    }
}
- (YWMessage *)createMessageWithContent:(NSString *)content title:(NSString*)title fromUserId:(NSInteger)fromUserId messageType:(YWMessageType)messageType relationId:(NSString *)relationId relationType:(NSInteger)relationType{
    YWMessage *message = [[YWMessage alloc] init];
    
    message.sessionId = [NSString stringWithFormat:@"%ld",(long)fromUserId];
    message.sessionType = YWSessionTypeSingalChat;
    message.sessionName = @"推送的最新消息";
    
    message.messageId = [YWCommonTool randStringId];
    message.messageType = messageType;
    message.title = title;
    message.message = content;
    message.createTime = [[NSDate date] timeIntervalSince1970]*1000;
    
    message.fromUserId = fromUserId;
    YWLoginUser *currentUser = [YWLoginTool loginUser];
    message.toUserId = currentUser.uniqueId;
    message.isRead = NO;
    message.relationId = relationId;
    message.relationType = relationType;
    return message;
}
//维保任务
- (NSString *)requestUrl:(YWMaintenanceType)maintenanceTypeId{
    NSString *url = nil;
//    if (maintenanceTypeId == YWMaintenanceTypePatrol) {
//        url = [YWCommonTool getPatrolMissionUrl];
//    }else
        if (maintenanceTypeId == YWMaintenanceTypeDetection){
        url = [YWCommonTool getDetectionMissionUrl];
    }else if (maintenanceTypeId == YWMaintenanceTypeUpkeep){
        url = [YWCommonTool getUpkeepMissionUrl];
    }
    return url;
}

//临时任务
- (void)requestWorkTask:(NSInteger)workTaskId{
    [[YWTempMissionManager shareManager] requestTempMission:0 WithComplete:^(BOOL success) {
        if (success) {
            NSArray *temMissionArr = [[YWTempMissionManager shareManager] tempMissionOfTypeWithMeOrOthers:YWTempMissionTypeOfAll page:1];
            __block YWTempMission *tempMission = nil;
            [temMissionArr enumerateObjectsUsingBlock:^(YWTempMission*  _Nonnull mission, NSUInteger idx, BOOL * _Nonnull stop) {
                if (mission.tempMissionId == workTaskId) {
                    tempMission = mission;
                    *stop = YES;
                }
            }];
            if (tempMission) {
                NSString *content = tempMission.content;//[NSString stringWithFormat:@"#%@#%@",tempMission.title,tempMission.content];
                YWMessage * message = [self createMessageWithContent:content title:tempMission.title fromUserId:tempMission.fromUser.uniqueId messageType:YWMessageTypeWorkNotice relationId:[NSString stringWithFormat:@"%ld",(long)tempMission.tempMissionId] relationType:tempMission.tempMissionId];
                [self performSelectorOnMainThread:@selector(saveAndPostNotificationWithMessage:) withObject:message waitUntilDone:NO];
            }
        }
        
    }];
}
//临时任务通知完成的回复
- (void)requestReplyOfWorkTask:(YWPushWorkTaskFinish*)workTaskFinish{
    
    [[YWTempMissionManager shareManager] requestTempMission:0 WithComplete:^(BOOL success) {
        if (success) {
            NSArray *recordArr = [[YWTempMissionManager shareManager] tempMissionRecordsWithNoReadStatus];
            __block YWTempMissionRecord *tempMissionRecord = nil;
            [recordArr enumerateObjectsUsingBlock:^(YWTempMissionRecord*  _Nonnull record, NSUInteger idx, BOOL * _Nonnull stop) {
                if (record.recordId == workTaskFinish.recordId) {
                    tempMissionRecord = record;
                }
            }];
            if (tempMissionRecord) {
                NSString *content = [NSString stringWithFormat:@"%@回复: %@",tempMissionRecord.executor.userName,tempMissionRecord.recordNote];
                YWMessage * message = [self createMessageWithContent:content title:@"临时任务回复" fromUserId:tempMissionRecord.executor.uniqueId messageType:YWMessageTypeWorkNoticeReply relationId:[NSString stringWithFormat:@"%ld",(long)tempMissionRecord.workTaskId] relationType:tempMissionRecord.workTaskId];
                if (tempMissionRecord.executor.uniqueId != [YWLoginTool loginUser].uniqueId){
                    [self performSelectorOnMainThread:@selector(saveAndPostNotificationWithMessage:) withObject:message waitUntilDone:NO];
                }else{
                    [self performSelectorOnMainThread:@selector(updateMessage:) withObject:message waitUntilDone:NO];
                }
                
            }
        }
        
    }];
}
//公告
- (void)requestPublicNotice:(YWPushWorkNotice*)workNotice{
    [[YWNoticeManager shareManager] requestNoticeList:0 WithComplete:^(BOOL success) {
        if (success) {
            NSArray *noticeArray = [[YWNoticeManager shareManager]noticesOfTypeWithMeOrOthers:YWNoticeTypeOfAll page:1];
            __block YWNotice *myNotice = nil;
            [noticeArray enumerateObjectsUsingBlock:^(YWNotice*  _Nonnull notice, NSUInteger idx, BOOL * _Nonnull stop) {
                if (workNotice.workNoticeId == notice.noticeId) {
                    myNotice = notice;
                    *stop = YES;
                }
            }];
            if (myNotice) {
                NSString *content = myNotice.content;//[NSString stringWithFormat:@"#%@#%@",myNotice.title,myNotice.content];
                YWMessage * message = [self createMessageWithContent:content title:myNotice.title fromUserId:myNotice.fromUser.uniqueId messageType:YWMessageTypePublicNotice relationId:[NSString stringWithFormat:@"%ld",(long)myNotice.noticeId] relationType:myNotice.noticeId];
                [self performSelectorOnMainThread:@selector(saveAndPostNotificationWithMessage:) withObject:message waitUntilDone:NO];
            }
        }
        
    }];
}
- (NSArray *)getMessageByFromUser{
    return [[YWDBTool shareDBTool] getMessageByFromUser];
}

- (NSArray *)getMessageWithFromUserId:(NSInteger)userId page:(NSInteger)page{
    return [[YWDBTool shareDBTool] getMessageWithFromUserId:userId page:page];
}

- (BOOL)setMessageIsReadWith:(NSArray *)messageArray{
    return [[YWDBTool shareDBTool] setMessageIsReadWith:messageArray];
}

- (NSArray *)messageListViewWithPage:(NSInteger)page{
    return [[YWDBTool shareDBTool] messageListViewWithPage:page];
}

-(NSArray *)publicNoticeAndTempMissionDataWithPage:(NSInteger)page{
    return [[YWDBTool shareDBTool] publicNoticeAndTempMissionDataWithPage:page];
}
@end
